<?php
require_once ('library/Model/BaseModel.php');

/**
 *
 * @author Nguyen
 *        
 */
class Customer extends BaseModel implements IModel {
	/**
	 *
	 * @param unknown $entity        	
	 * @return number
	 */
	public function register($entity) {
		// kiem tra sdt da ton tai hay chua
		$sqlcheck = 'select * from customer where numberphone = ' . $entity ['numberphone'];
		$listphone = $this->db->fetchAll ( $sqlcheck );
		$code = iKey::generate ( 4 ); // tao code
		if (count ( $listphone ) > 0)
			// neu ton tai numberphone nay thi tra ve 1
			return 1;
		else {
			$sql = 'insert into customer(username,password,code,numberphone,email,address,image,updatetime,latitude,longtitude,status)
					values ( "' . $entity ['username'] . '","' . $entity ['password'] . '","' . $code . '","' . $entity ['numberphone'] . '","' . $entity ['email'] . '","' . $entity ['address'] . '","",0,0,0,0 )';
			$c = $this->db->execute ( $sql );
			if ($c > 0)
				return $code; // tra ve code khi insert thanh cong
			else
				return 0; // ko insert thanh cong
		}
	}
	/**
	 */
	public function checkcustomer($phone, $pass) {
		$sql = 'select * from customer where numberphone = "' . $phone . '" and password="' . iKey::encrypepass ( $pass ) . '" and status = 1';
		$result = $this->db->fetchAll ( $sql );
		if (count ( $result ) > 0) {
			return $result [0];
		}
		return null;
	}
	/**
	 *
	 * @param unknown $phone        	
	 * @param unknown $code        	
	 * @return number string
	 */
	public function checkactivecustomer($phone, $code) {
		$result = array (
				'result' => Config::RESULT_FAIL,
				'data' => "" 
		);
		$sql = 'select * from customer where numberphone = "' . $phone . '" and code ="' . $code . '"';
		$x = $this->db->fetchRow ( $sql );
		if (isset ( $x )) {
			$result ['result'] = Config::RESULT_SUCCESS;
			$result ['data'] = $x;
			$result ['message'] = "Active thanh cong";
			$isql = 'update customer set code = "", status=1 where numberphone = ' . $phone;
			$c = $this->db->execute ( $isql );
		} else {
			$code = iKey::generate ( 4 );
			$isql = 'update customer set code = "' . $code . '" where numberphone = ' . $phone;
			$c = $this->db->execute ( $isql );
			$result ['result'] = Config::RESULT_FAIL;
			$result ['data'] = $code;
			$result ['message'] = "Ban da nhap sai code";
		}
		return $result;
	}
}

?>